package com.hmall.order.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hmall.feign.client.ItemClient;
import com.hmall.feign.client.UserClient;
import com.hmall.item.pojo.Item;
import com.hmall.order.dto.OrderDto;
import com.hmall.order.mapper.OrderDetailMapper;
import com.hmall.order.mapper.OrderLogisticsMapper;
import com.hmall.order.mapper.OrderMapper;
import com.hmall.order.pojo.Order;
import com.hmall.order.pojo.OrderDetail;
import com.hmall.order.pojo.OrderLogistics;
import com.hmall.order.service.OrderService;
import com.hmall.order.utils.RequestHolder;
import com.hmall.user.pojo.Address;
import io.seata.spring.annotation.GlobalTransactional;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author mengjie
 */
@Service
public class OrderServiceImpl implements OrderService {
    private static final Logger log = LoggerFactory.getLogger(OrderServiceImpl.class);
    @Autowired
    OrderMapper orderMapper;  //操作订单
    @Autowired
    ItemClient itemClient; //远程调用商品微服务feign接口
    @Autowired
    OrderDetailMapper orderDetailMapper; //操作订单详情
    @Autowired
    OrderLogisticsMapper orderLogisticsMapper; //操作订单物流
    @Autowired
    UserClient userClient; //远程调用用户微服务接口

    /**
     * 分页查询订单
     *
     * @param pageNum  页码
     * @param pageSize 每页大小
     * @return 订单列表
     */
    @Override
    public IPage<Order> pageQuery(int pageNum, int pageSize) {
        return orderMapper
                .selectPage(new Page<>(pageNum, pageSize), null);
    }

    @Override
    @GlobalTransactional
    public String insert(OrderDto orderDto) {
        //远程调用库存服务 根据商品id查询商品信息
        Item item = itemClient.queryItemById(orderDto.getItemId());
        //添加订单信息，tb_order
        Order order = new Order();
        order.setTotalFee(item.getPrice() * orderDto.getNum()); //总金额
        order.setPaymentType(orderDto.getPaymentType()); //支付方式
        order.setUserId(Long.valueOf(RequestHolder.getUserId())); //用户id
        order.setStatus(1); //订单状态 1未付款
        order.setCreateTime(new Date()); //创建时间
        order.setUpdateTime(new Date()); //更新时间
        orderMapper.insert(order);

        //2. 添加订单详情项，tb_order_detail
        OrderDetail orderDetail = new OrderDetail();
        orderDetail.setOrderId(order.getId()); //订单ID
        orderDetail.setItemId(orderDto.getItemId()); //商品id
        orderDetail.setNum(orderDto.getNum()); //商品数量
        orderDetail.setName(item.getName()); //商品名称
        orderDetail.setSpec(item.getSpec()); //商品规格
        orderDetail.setPrice(item.getPrice()); //商品价格
        orderDetail.setImage(item.getImage());
        orderDetail.setCreateTime(new Date()); //创建时间
        orderDetail.setUpdateTime(new Date()); //更新时间

        orderDetailMapper.insert(orderDetail);
        //3. 添加订单物流信息，tb_order_logistics
        //远程调用用户微服务中的功能，根据地址id查询地址信息，由于用户微服务中涉及到鉴权，所有需要传入用户id
        Address address = userClient.findAddressById(orderDto.getAddressId(), RequestHolder.getUserId());
        OrderLogistics orderLogistics = new OrderLogistics();
        orderLogistics.setOrderId(order.getId()); //订单id
        orderLogistics.setContact(address.getContact()); //收货人
        orderLogistics.setMobile(address.getMobile()); //手机号
        orderLogistics.setProvince(address.getProvince()); //省
        orderLogistics.setCity(address.getCity()); //市
        orderLogistics.setTown(address.getTown()); //区
        orderLogistics.setStreet(address.getStreet()); //街道
        orderLogistics.setCreateTime(new Date()); //创建时间
        orderLogistics.setUpdateTime(new Date()); //更新时间
        //添加物流信息
        orderLogisticsMapper.insert(orderLogistics);

        //4. 扣减订单的库存-1，tb_item
        itemClient.updateStock(orderDto.getItemId(), orderDto.getNum());

        return String.valueOf(order.getId());
    }

    //根据订单id查询订单信息
    @Override
    public Order getById(Long id) {
        return orderMapper.selectById(id);
    }

    //根据订单id查询商品购买的订单信息
    @Override
    public OrderDetail getDetail(Long id) {
        return orderMapper.selectOrderDetailById(id);
    }

    /**
     * 根据订单id修改订单状态
     *
     * @param order
     */
    @Override
    public void updateOrderStatus(Order order) {
        orderMapper.updateById(order);
    }

    @Override
    public String update(Order order) {
        orderMapper.updateById(order);
        return order.getId().toString();
    }
}